/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.helper.list;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;
import java.util.Objects;

/**
 * 识别页面tab的PageSlider
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/4/28
 */
public class PageProvider extends PageSliderProvider {
    private final AbilitySlice slice;
    private final List<TabListItemProvider> list;

    public PageProvider(AbilitySlice slice, List<TabListItemProvider> list) {
        this.slice = slice;
        this.list = list;
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        final ListContainer component = new ListContainer(slice);
        component.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT,
                DirectionalLayout.LayoutConfig.MATCH_PARENT
        ));
        if (list == null || list.size() == 0) {
            return null;
        }
        component.setItemProvider(list.get(i));
        componentContainer.addComponent(component);
        return component;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        if (o == null) {
            return;
        }
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        if (o == null) {
            return false;
        }
        return Objects.equals(component, o);
    }
}
